﻿using PC_Touch.DataOP;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.Threading;

namespace PC_Touch
{
    public partial class SettingsQF : Form
    {
        public SettingsQF()
        {
            InitializeComponent();
            x = this.Width;
            y = this.Height;
            setTag(this);
            this.Resize += new EventHandler(this.Form_Resize);
            setZH_EN();
        }
        private void setZH_EN()
        {
            if (LanguageHelper.zh_enFlag == "中文")
            {
            }
            else
            {
                uiTitlePanel1.Text = "Manual air valve control";
                uiTitlePanel2.Text = "Manual water valve control";
                uiMarkLabel1.Text = "PneumaticValve1";
                uiMarkLabel2.Text = "PneumaticValve2";
                uiMarkLabel4.Text = "PneumaticValve3";
                uiMarkLabel3.Text = "PneumaticValve4";
                uiMarkLabel6.Text = "PneumaticValve5";
                uiMarkLabel5.Text = "PneumaticValve6";
                uiMarkLabel12.Text = "PneumaticValve7";
                uiMarkLabel11.Text = "PneumaticValve8";
                uiMarkLabel10.Text = "PneumaticValve9";
                uiMarkLabel9.Text = "PneumaticValve10";
                uiMarkLabel8.Text = "PneumaticValve11";
                uiMarkLabel7.Text = "PneumaticValve12";
                qf13label.Text = "PneumaticValve13";
                uiMarkLabel24.Text = "WaterValve1";
                uiMarkLabel23.Text = "WaterValve2";
                uiMarkLabel22.Text = "WaterValve3";
                uiMarkLabel21.Text = "WaterValve4";
                uiMarkLabel20.Text = "WaterValve5";
                uiMarkLabel19.Text = "WaterValve6";
                uiMarkLabel18.Text = "WaterValve7";
                uiMarkLabel17.Text = "WaterValve8";
                uiMarkLabel16.Text = "WaterValve9";
                uiMarkLabel15.Text = "WaterValve10";
                uiMarkLabel14.Text = "WaterValve11";
                uiMarkLabel13.Text = "WaterValve12";


            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ReadPLCStatuesOnce();

            //监听事件初始化
            //PLCHelper.TriggerEventInit();
            //PLC轮询读取触发信号

        }
        public static bool[] ByteToBoolArray(byte b)
        {
            bool[] bits = new bool[8];

            for (int i = 0; i < 8; i++)
            {
                // 检查当前位是否为1，并将结果存储到对应的数组位置
                bits[i] = (b & (1 << (7 - i))) != 0;
            }

            return bits;
        }
        private void ReadPLCStatuesOnce()
        {
            if (PLCHelper.siemensConnect.IsSuccess)
            {
               // var by = PLCHelper.siemensTcpNet.ReadBool("Q104.4",12).Content;//读取气阀状态
                var q104_107 = PLCHelper.siemensTcpNet.Read("Q104",9).Content;
                var q104 = ByteToBoolArray(q104_107[0]);
                var q105 = ByteToBoolArray(q104_107[1]);
                var q106 = ByteToBoolArray(q104_107[2]);
                var q107 = ByteToBoolArray(q104_107[3]);
                var q108 = ByteToBoolArray(q104_107[8]);

                qf1.Active = q104[4];  // PLCHelper.siemensTcpNet.ReadBool("Q104.4").Content; 
                qf2.Active = q104[5];  //PLCHelper.siemensTcpNet.ReadBool("Q104.5").Content;
                qf3.Active = q104[6];  //PLCHelper.siemensTcpNet.ReadBool("Q104.6").Content;
                qf4.Active = q104[7];  //PLCHelper.siemensTcpNet.ReadBool("Q104.7").Content;
                qf5.Active = q105[0];  //PLCHelper.siemensTcpNet.ReadBool("Q105.0").Content;
                qf6.Active = q105[1];  //PLCHelper.siemensTcpNet.ReadBool("Q105.1").Content;
                qf7.Active = q105[2];  //PLCHelper.siemensTcpNet.ReadBool("Q105.2").Content;
                qf8.Active = q105[3];  //PLCHelper.siemensTcpNet.ReadBool("Q105.3").Content;
                qf9.Active = q105[4];  //PLCHelper.siemensTcpNet.ReadBool("Q105.4").Content;
                qf10.Active = q105[5];  // PLCHelper.siemensTcpNet.ReadBool("Q105.5").Content;
                qf11.Active = q105[6];  // PLCHelper.siemensTcpNet.ReadBool("Q105.6").Content;
                qf12.Active = q105[7];  // PLCHelper.siemensTcpNet.ReadBool("Q105.7").Content;
                qf13.Active = q108[0];// PLCHelper.siemensTcpNet.ReadBool("Q108.0").Content;

                SF1.Active = q106[0];  // PLCHelper.siemensTcpNet.ReadBool("Q106.0").Content;
                SF2.Active = q106[1];  // PLCHelper.siemensTcpNet.ReadBool("Q106.1").Content;
                SF3.Active = q106[2];  // PLCHelper.siemensTcpNet.ReadBool("Q106.2").Content;
                SF4.Active = q106[3];  // PLCHelper.siemensTcpNet.ReadBool("Q106.3").Content;
                SF5.Active = q106[4];  // PLCHelper.siemensTcpNet.ReadBool("Q106.4").Content;
                SF6.Active = q106[5];  // PLCHelper.siemensTcpNet.ReadBool("Q106.5").Content;
                SF7.Active = q106[6];  // PLCHelper.siemensTcpNet.ReadBool("Q106.6").Content;
                SF8.Active = q106[7];  // PLCHelper.siemensTcpNet.ReadBool("Q106.7").Content;
                SF9.Active = q107[0];  // PLCHelper.siemensTcpNet.ReadBool("Q107.0").Content;
                SF10.Active = q107[1];  // PLCHelper.siemensTcpNet.ReadBool("Q107.1").Content;
                SF11.Active = q107[2];  //PLCHelper.siemensTcpNet.ReadBool("Q107.2").Content;
                SF12.Active = q107[3];  // PLCHelper.siemensTcpNet.ReadBool("Q107.3").Content;


            }
            else 
            {
                MessageBox.Show("PLC not connect!");
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            TaskManagement taskManagement = new TaskManagement();
            taskManagement.Show();
        }

        #region 控件缩放
        private float x = 1;
        private float y = 1;
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        private void Form_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / x;
            float newy = (this.Height) / y;
            setControls(newx, newy, this);
        }
        #endregion
        private void uiSwitch12_ValueChanged(object sender, bool value)
        {
            if (qf5.Active)//气阀5控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.7.6", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.7.6", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.7.7", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.7.7", false);
            }
        }
        private void uiSwitch1_ValueChanged(object sender, bool value)
        {
            if (qf1.Active)//气阀1控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.6.6", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.6.6", false);
            }
            else 
            {
                PLCHelper.siemensTcpNet.Write("DB10.6.7", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.6.7", false);
            }
        }

        private void uiSwitch4_ValueChanged(object sender, bool value)
        {
            if (qf2.Active)//气阀2控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.7.0", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.7.0", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.7.1", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.7.1", false);
            }
        }

        private void qf3_ValueChanged(object sender, bool value)
        {
            if (qf3.Active)//气阀3控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.7.2", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.7.2", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.7.3", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.7.3", false);
            }
        }

        private void qf4_ValueChanged(object sender, bool value)
        {
            if (qf4.Active)//气阀4控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.7.4", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.7.4", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.7.5", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.7.5", false);
            }
        }

        private void qf6_ValueChanged(object sender, bool value)
        {
            if (qf6.Active)//气阀6控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.8.0", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.8.0", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.8.1", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.8.1", false);
            }
        }

        private void qf7_ValueChanged(object sender, bool value)
        {
            if (qf7.Active)//气阀7控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.11.6", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.11.6", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.11.7", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.11.7", false);
            }
        }

        private void qf8_ValueChanged(object sender, bool value)
        {
            if (qf8.Active)//气阀8控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.12.0", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.12.0", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.12.1", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.12.1", false);
            }

        }

        private void qf9_ValueChanged(object sender, bool value)
        {
            if (qf9.Active)//气阀9控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.12.2", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.12.2", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.12.3", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.12.3", false);
            }
        }

        private void qf10_ValueChanged(object sender, bool value)
        {
            if (qf10.Active)//气阀10控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.12.4", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.12.4", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.12.5", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.12.5", false);
            }
        }

        private void qf11_ValueChanged(object sender, bool value)
        {
            if (qf11.Active)//气阀11控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.12.6", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.12.6", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.12.7", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.12.7", false);
            }
        }

        private void qf12_ValueChanged(object sender, bool value)
        {
            if (qf12.Active)//气阀12控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.13.0", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.13.0", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.13.1", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.13.1", false);
            }
        }

        private void uiSwitch1_ValueChanged_1(object sender, bool value)
        {
            if (qf13.Active)//气阀13控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.14.2", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.14.2", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.14.3", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.14.3", false);
            }
        }

        private void SF1_ValueChanged(object sender, bool value)
        {
            if (SF1.Active)//水阀1控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.4.2", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.4.2", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.4.3", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.4.3", false);
            }
        }

        private void SF2_ValueChanged(object sender, bool value)
        {
            if (SF2.Active)//水阀2控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.4.4", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.4.4", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.4.5", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.4.5", false);
            }
        }

        private void SF3_ValueChanged(object sender, bool value)
        {
            if (SF3.Active)//水阀3控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.4.6", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.4.6", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.4.7", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.4.7", false);
            }
        }

        private void SF4_ValueChanged(object sender, bool value)
        {
            if (SF4.Active)//水阀4控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.5.0", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.5.0", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.5.1", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.5.1", false);
            }
        }

        private void SF5_ValueChanged(object sender, bool value)
        {
            if (SF5.Active)//水阀5控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.5.2", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.5.2", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.5.3", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.5.3", false);
            }
        }

        private void SF6_ValueChanged(object sender, bool value)
        {
            if (SF6.Active)//水阀6控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.5.4", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.5.4", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.5.5", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.5.5", false);
            }
        }

        private void SF7_ValueChanged(object sender, bool value)
        {
            if (SF7.Active)//水阀7控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.9.2", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.9.2", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.9.3", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.9.3", false);
            }
        }

        private void SF8_ValueChanged(object sender, bool value)
        {
            if (SF8.Active)//水阀8控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.9.4", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.9.4", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.9.5", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.9.5", false);
            }
        }

        private void SF9_ValueChanged(object sender, bool value)
        {
            if (SF9.Active)//水阀9控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.9.6", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.9.6", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.9.7", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.9.7", false);
            }
        }

        private void SF10_ValueChanged(object sender, bool value)
        {
            if (SF10.Active)//水阀10控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.10.0", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.10.0", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.10.1", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.10.1", false);
            }
        }

        private void SF11_ValueChanged(object sender, bool value)
        {
            if (SF11.Active)//水阀11控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.10.2", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.10.2", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.10.3", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.10.3", false);
            }
        }

        private void SF12_ValueChanged(object sender, bool value)
        {
            if (SF12.Active)//水阀12控制
            {
                PLCHelper.siemensTcpNet.Write("DB10.10.4", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.10.4", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.10.5", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.10.5", false);
            }
        }
    }
}
